﻿using System;
using Glue;
using Xunit;

namespace GluePerformanceTesting
{
    public class Flattening
    {
        private readonly PerformanceMeasure measurer;

        private class GuiPerson { public String City { get; set; } }
        private class DomainPerson { public DomainAddress Address { get; set; } }
        public class DomainAddress { public String City { get; set;}}

        public Flattening()
        {
            measurer = PerformanceMeasure.GetInstance();
        }

        [Fact]
        public void Run_Simplified_mapping_end_to_end()
        {
            var domainPerson = new DomainPerson {Address = new DomainAddress{City = "Trondheim"}};
            measurer.RunLong("Flattening, end to end", () =>
            {
                var mapping = new Mapping<DomainPerson, GuiPerson>();
                var flattenedMapping = new Mapping<DomainAddress, GuiPerson>();
                flattenedMapping.Relate(domain=>domain.City,gui=>gui.City);
                mapping.Flatten(domain=>domain.Address, flattenedMapping);
                mapping.Map(domainPerson, new GuiPerson());
            });
        }

        [Fact]
        public void Run_Simplified_flattening_mapping_only()
        {
            var mapping = new Mapping<DomainPerson, GuiPerson>();
            var flattenedMapping = new Mapping<DomainAddress, GuiPerson>();
            flattenedMapping.Relate(domain => domain.City, gui => gui.City);
            mapping.Flatten(domain => domain.Address, flattenedMapping);
            var domainPerson = new DomainPerson { Address = new DomainAddress { City = "Trondheim" } };
            measurer.Run("Flattening, mapping only", () => mapping.Map(domainPerson, new GuiPerson()));
        }
    }
}
